# include "Equipment.h"

# include "Renderer.h"

# include "Hero.h"
# include "Armor.h"
# include "Weapon.h"
# include "Shield.h"
# include "Jewel.h"

Equipment::Equipment() : myBodyArmor( 0), myFeetArmor( 0), myHeadArmor( 0), myHipsArmor( 0), myArmsArmor( 0), myWeapon( 0), myShield( 0)
{

}

Equipment::~Equipment()
{
	if ( myWeapon != NULL )
	{
		myRenderer->deleteEntity( myWeaponMesh);
	}

	if ( myShield != NULL )
	{
		myRenderer->deleteEntity( myShieldMesh);
	}
	
	if ( myHeadArmor != NULL )
	{
		myRenderer->deleteEntity( myHeadMesh);
	}

	if ( myBodyArmor != NULL )
	{
		myRenderer->deleteEntity( myBodyMesh);
	}

	if ( myArmsArmor != NULL )
	{
		myRenderer->deleteEntity( myArmsMesh);
	}

	if ( myHipsArmor != NULL )
	{
		myRenderer->deleteEntity( myHipsMesh);
	}

	if ( myFeetArmor != NULL )
	{
		myRenderer->deleteEntity( myFeetMesh);
	}
}

unsigned int Equipment::getDamageMin()
{
	return myDamageMin;
}

unsigned int Equipment::getDamageMax()
{
	return myDamageMax;
}

unsigned int Equipment::getChance()
{
	return myChance;
}

unsigned int Equipment::getArmor()
{
	return myArmor;
}

void Equipment::init( Renderer* renderer, Hero* hero)
{
	myRenderer = renderer;
	myHero = hero;

	myDamageMin = 1;
	myDamageMax = 1;
	myChance = 0;
	myArmor = 0;
}

void Equipment::equip( Weapon* weapon)
{
	Ogre::SceneNode* bodyNodeTmp = myHero->getBodyNode();
	
	switch ( weapon->getHand() )
	{
		case ONE :
		{
			if ( myWeapon != NULL )
			{
				if ( myWeapon->getName() != weapon->getName() )
				{
					bodyNodeTmp->detachObject( myWeaponMesh);
					myRenderer->deleteEntity( myWeaponMesh);
					myWeaponMesh = myRenderer->createEntity( weapon->getName(), weapon->getMeshName());
					myWeaponMesh->setQueryFlags( ME_FLAG);
					myWeapon = weapon;
					bodyNodeTmp->attachObject( myWeaponMesh);

					myDamageMin = myWeapon->getDamageMin();
					myDamageMax = myWeapon->getDamageMax();
				}

				else
				{
					bodyNodeTmp->detachObject( myWeaponMesh);
					myRenderer->deleteEntity( myWeaponMesh);
					myWeapon = NULL;
					myWeaponMesh = NULL;

					myDamageMin = 1;
					myDamageMax = 1;
				}
			}

			else
			{
				myWeaponMesh = myRenderer->createEntity( weapon->getName(), weapon->getMeshName());
				myWeaponMesh->setQueryFlags( ME_FLAG);
				myWeapon = weapon;
				bodyNodeTmp->attachObject( myWeaponMesh);

				myDamageMin = myWeapon->getDamageMin();
				myDamageMax = myWeapon->getDamageMax();
			}
		}
		break;

		case TWO :
		{
			if ( myWeapon != NULL )
			{
				if ( myWeapon->getName() != weapon->getName() )
				{
					if ( myShield != NULL )
					{
						myArmor -= myShield->getArmor();
						myChance = 0;
						
						bodyNodeTmp->detachObject( myShieldMesh);
						myRenderer->deleteEntity( myShieldMesh);
						myShield = NULL;
						myShieldMesh = NULL;

						bodyNodeTmp->detachObject( myWeaponMesh);
						myRenderer->deleteEntity( myWeaponMesh);
						myWeaponMesh = myRenderer->createEntity( weapon->getName(), weapon->getMeshName());
						myWeaponMesh->setQueryFlags( ME_FLAG);
						myWeapon = weapon;
						bodyNodeTmp->attachObject( myWeaponMesh);

						myDamageMin = myWeapon->getDamageMin();
						myDamageMax = myWeapon->getDamageMax();
					}

					else
					{
						bodyNodeTmp->detachObject( myWeaponMesh);
						myRenderer->deleteEntity( myWeaponMesh);
						myWeaponMesh = myRenderer->createEntity( weapon->getName(), weapon->getMeshName());
						myWeaponMesh->setQueryFlags( ME_FLAG);
						myWeapon = weapon;
						bodyNodeTmp->attachObject( myWeaponMesh);

						myDamageMin = myWeapon->getDamageMin();
						myDamageMax = myWeapon->getDamageMax();
					}
				}

				else
				{
					bodyNodeTmp->detachObject( myWeaponMesh);
					myRenderer->deleteEntity( myWeaponMesh);
					myWeapon = NULL;
					myWeaponMesh = NULL;

					myDamageMin = 1;
					myDamageMax = 1;
				}
			}

			else
			{
				if ( myShield != NULL )
					{
						myArmor -= myShield->getArmor();
						myChance = 0;

						bodyNodeTmp->detachObject( myShieldMesh);
						myRenderer->deleteEntity( myShieldMesh);
						myShield = NULL;
						myShieldMesh = NULL;

						myWeaponMesh = myRenderer->createEntity( weapon->getName(), weapon->getMeshName());
						myWeaponMesh->setQueryFlags( ME_FLAG);
						myWeapon = weapon;
						bodyNodeTmp->attachObject( myWeaponMesh);

						myDamageMin = myWeapon->getDamageMin();
						myDamageMax = myWeapon->getDamageMax();
					}

					else
					{
						myWeaponMesh = myRenderer->createEntity( weapon->getName(), weapon->getMeshName());
						myWeaponMesh->setQueryFlags( ME_FLAG);
						myWeapon = weapon;
						bodyNodeTmp->attachObject( myWeaponMesh);

						myDamageMin = myWeapon->getDamageMin();
						myDamageMax = myWeapon->getDamageMax();
					}
			}
		}
		break;
	}
}


void Equipment::equip( Shield* shield)
{
	Ogre::SceneNode* bodyNodeTmp = myHero->getBodyNode();

	if ( myShield != NULL )
	{
		if ( myShield->getName() != shield->getName() )
		{
			myArmor -= myShield->getArmor();
			
			bodyNodeTmp->detachObject( myShieldMesh);
			myRenderer->deleteEntity( myShieldMesh);
			myShieldMesh = myRenderer->createEntity( shield->getName(), shield->getMeshName());
			myShieldMesh->setQueryFlags( ME_FLAG);
			myShield = shield;
			bodyNodeTmp->attachObject( myShieldMesh);

			myArmor += myShield->getArmor();
			myChance = myShield->getChance();
		}

		else
		{
			myArmor -= myShield->getArmor();
			myChance = 0;
			
			bodyNodeTmp->detachObject( myShieldMesh);
			myRenderer->deleteEntity( myShieldMesh);
			myShield = NULL;
			myShieldMesh = NULL;
		}
	}

	else
	{
		if ( myWeapon != NULL )
		{
			switch ( myWeapon->getHand() )
			{
				case ONE:
				{
					myShieldMesh = myRenderer->createEntity( shield->getName(), shield->getMeshName());
					myShieldMesh->setQueryFlags( ME_FLAG);
					myShield = shield;
					bodyNodeTmp->attachObject( myShieldMesh);

					myArmor += myShield->getArmor();
					myChance = myShield->getChance();
				}
				break;

				case TWO:
				{
					bodyNodeTmp->detachObject( myWeaponMesh);
					myRenderer->deleteEntity( myWeaponMesh);
					myWeapon = NULL;
					myWeaponMesh = NULL;

					myDamageMin = 1;
					myDamageMax  = 1;
					
					myShieldMesh = myRenderer->createEntity( shield->getName(), shield->getMeshName());
					myShieldMesh->setQueryFlags( ME_FLAG);
					myShield = shield;
					bodyNodeTmp->attachObject( myShieldMesh);

					myArmor += myShield->getArmor();
					myChance = myShield->getChance();
				}	
				break;
			}
		}

		else
		{
			myShieldMesh = myRenderer->createEntity( shield->getName(), shield->getMeshName());
			myShieldMesh->setQueryFlags( ME_FLAG);
			myShield = shield;
			bodyNodeTmp->attachObject( myShieldMesh);

			myArmor += myShield->getArmor();
			myChance = myShield->getChance();
		}
	}
}

void Equipment::equip( Armor* armor)
{
	Ogre::SceneNode* bodyNodeTmp = myHero->getBodyNode();
	
	switch ( armor->getType() )
	{
		case HEAD_ARMOR:
		{
			if ( myHeadArmor != NULL )
			{
				if ( myHeadArmor->getName() != armor->getName() )
				{
					myArmor -= myHeadArmor->getArmor();

					bodyNodeTmp->detachObject( myHeadMesh);
					myRenderer->deleteEntity( myHeadMesh);
					myHeadMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
					myHeadMesh->setQueryFlags( ME_FLAG);
					myHeadArmor = armor;
					bodyNodeTmp->attachObject( myHeadMesh);

					myArmor += myHeadArmor->getArmor();
				}

				else
				{
					myArmor += myHeadArmor->getArmor();
					
					bodyNodeTmp->detachObject( myHeadMesh);
					myRenderer->deleteEntity( myHeadMesh);
					myHeadArmor = NULL;
					myHeadMesh = NULL;
				}
			}
			
			else
			{
				myHeadMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
				myHeadMesh->setQueryFlags( ME_FLAG);
				myHeadArmor = armor;
				bodyNodeTmp->attachObject( myHeadMesh);

				myArmor += myHeadArmor->getArmor();

			}
		}
		break;
		
		case BODY_ARMOR:
		{
			if ( myBodyArmor != NULL )
			{
				if ( myBodyArmor->getName() != armor->getName() )
				{
					myArmor -= myBodyArmor->getArmor();
					
					bodyNodeTmp->detachObject( myBodyMesh);
					myRenderer->deleteEntity( myBodyMesh);
					myBodyMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
					myBodyMesh->setQueryFlags( ME_FLAG);
					myBodyArmor = armor;
					bodyNodeTmp->attachObject( myBodyMesh);

					myArmor += myBodyArmor->getArmor();
				}

				else
				{
					myArmor -= myBodyArmor->getArmor();

					bodyNodeTmp->detachObject( myBodyMesh);
					myRenderer->deleteEntity( myBodyMesh);
					myBodyArmor = NULL;
					myBodyMesh = NULL;
				}
			}
			
			else
			{
				myBodyMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
				myBodyMesh->setQueryFlags( ME_FLAG);
				myBodyArmor = armor;
				bodyNodeTmp->attachObject( myBodyMesh);

				myArmor += myBodyArmor->getArmor();
			}
		}
		break;

		case ARMS_ARMOR:
		{
			if ( myArmsArmor != NULL )
			{
				if ( myArmsArmor->getName() != armor->getName() )
				{
					myArmor -= myArmsArmor->getArmor();

					bodyNodeTmp->detachObject( myArmsMesh);
					myRenderer->deleteEntity( myArmsMesh);
					myArmsMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
					myArmsMesh->setQueryFlags( ME_FLAG);
					myArmsArmor = armor;
					bodyNodeTmp->attachObject( myArmsMesh);

					myArmor += myArmsArmor->getArmor();
				}

				else
				{
					myArmor -= myArmsArmor->getArmor();
					
					bodyNodeTmp->detachObject( myArmsMesh);
					myRenderer->deleteEntity( myArmsMesh);
					myArmsArmor = NULL;
					myArmsMesh = NULL;
				}
			}
			
			else
			{
				myArmsMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
				myArmsMesh->setQueryFlags( ME_FLAG);
				myArmsArmor = armor;
				bodyNodeTmp->attachObject( myArmsMesh);

				myArmor += myArmsArmor->getArmor();
			}
		}
		break;

		case HIPS_ARMOR:
		{
			if ( myHipsArmor != NULL )
			{
				if ( myHipsArmor->getName() != armor->getName() )
				{
					myArmor -= myHipsArmor->getArmor();
					
					bodyNodeTmp->detachObject( myHipsMesh);
					myRenderer->deleteEntity( myHipsMesh);
					myHipsMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
					myHipsMesh->setQueryFlags( ME_FLAG);
					myHipsArmor = armor;
					bodyNodeTmp->attachObject( myHipsMesh);

					myArmor += myHipsArmor->getArmor();
				}

				else
				{
					myArmor -= myHipsArmor->getArmor();
					
					bodyNodeTmp->detachObject( myHipsMesh);
					myRenderer->deleteEntity( myHipsMesh);
					myHipsArmor = NULL;
					myHipsMesh = NULL;
				}
			}
			
			else
			{
				myHipsMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
				myHipsMesh->setQueryFlags( ME_FLAG);
				myHipsArmor = armor;
				bodyNodeTmp->attachObject( myHipsMesh);

				myArmor += myHipsArmor->getArmor();
			}
		}
		break;

		case FEET_ARMOR:
		{
			if ( myFeetArmor != NULL )
			{
				if ( myFeetArmor->getName() != armor->getName() )
				{
					myArmor -= myFeetArmor->getArmor();
					
					bodyNodeTmp->detachObject( myFeetMesh);
					myRenderer->deleteEntity( myFeetMesh);
					myFeetMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
					myFeetMesh->setQueryFlags( ME_FLAG);					
					myFeetArmor = armor;
					bodyNodeTmp->attachObject( myFeetMesh);

					myArmor += myFeetArmor->getArmor();
				}

				else
				{
					myArmor -= myFeetArmor->getArmor();
					
					bodyNodeTmp->detachObject( myFeetMesh);
					myRenderer->deleteEntity( myFeetMesh);
					myFeetArmor = NULL;
					myHipsMesh = NULL;
				}
			}
			
			else
			{
				myFeetMesh = myRenderer->createEntity( armor->getName(), armor->getMeshName());
				myFeetMesh->setQueryFlags( ME_FLAG);	
				myFeetArmor = armor;
				bodyNodeTmp->attachObject( myFeetMesh);

				myArmor += myFeetArmor->getArmor();
			}
		}
		break;
	}
}

void Equipment::equip( Jewel* jewel)
{
	Ogre::SceneNode* bodyNodeTmp = myHero->getBodyNode();
	
	switch ( jewel->getType() )
	{
		case NECKLACE:
		{
			if ( myNecklace != NULL )
			{
				if ( myNecklace->getName() != jewel->getName() )
				{
					myNecklace = jewel;
				}

				else
				{
					myNecklace = NULL;
				}
			}

			else
			{
				myNecklace = jewel;
			}
		}
		break;

		case RING:
		{
			if ( myRing1 != NULL )
			{
				if ( myRing1->getName() != jewel->getName() )
				{
					if ( myRing2 != NULL )
					{
						if ( myRing2->getName() != jewel->getName() )
						{
							myRing1 = myRing2;
							myRing2 = jewel;
						}

						else
						{
							myRing2 = NULL;
						}
					}

					else
					{
						myRing2 = jewel;
					}
				}

				else
				{
					myRing1 = NULL;
				}
			}

			else
			{
				myRing1 = jewel;
			}
		}
		break;
	}
}

Weapon* Equipment::findActualWeapon()
{
	return myWeapon;
}

Shield* Equipment::findActualShield()
{
	return myShield;
}

Armor* Equipment::findActualArmor( unsigned int type)
{
	switch ( type )
	{
		case HEAD_ARMOR:
		return myHeadArmor;
		break;

		case BODY_ARMOR:
		return myBodyArmor;
		break;

		case ARMS_ARMOR:
		return myArmsArmor;
		break;

		case HIPS_ARMOR:
		return myHipsArmor;
		break;

		case FEET_ARMOR:
		return myFeetArmor;
		break;

		default:
		return NULL;
		break;
	}
}

Jewel* Equipment::findActualJewel( unsigned int type)
{
	switch ( type )
	{
		case RING:
		return myRing1;
		break;

		case NECKLACE:
		return myNecklace;
		break;

		default:
		return NULL;
		break;
	}
}